package cn.jyc.ssm03.service;

import cn.jyc.ssm03.dao.CarMapper;
import cn.jyc.ssm03.pojo.Car;
import cn.jyc.ssm03.utils.JsonUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CarService {
    @Autowired
    private CarMapper cm;

    public String qryCar(Integer p1, Integer p2, Car car) {
        Map<String, Object> res = new HashMap<>();
        Page<Object> p = PageHelper.startPage(p1, p2);
        List<Car> cars = cm.qryCar(car);
        res.put("rows", cars);
        res.put("total", p.getTotal());
        return JsonUtil.objectToJson(res);
    }

    public String addCar(Car car) {
        Map<String, Object> res = new HashMap<>();
        try {
            cm.addCar(car);
        } catch (Exception e){
            res.put("errorMsg",e.getMessage());
        }
        return JsonUtil.objectToJson(res);
    }

    public String updCar(Car car) {
        Map<String, Object> res = new HashMap<>();
        try {
            cm.updCar(car);
        } catch (Exception e){
            res.put("errorMsg",e.getMessage());
        }
        return JsonUtil.objectToJson(res);
    }

    public String delCar(String idd) {
        Map<String, Object> res = new HashMap<>();
        try {
            cm.delCar(idd);
            res.put("success",true);
        } catch (Exception e){
            res.put("errorMsg",e.getMessage());
        }
        return JsonUtil.objectToJson(res);
    }
}
